#-*- coding:utf-8-*
# 打包成单exe文件 pyinstaller --onefile main.py
import pandas as pd
from docx import Document

# 读取 Excel 数据
data = pd.read_excel("变量1.xlsx")

# 准备 Word 模板
template_file = "测试.docx"




# 替换文本
def replace_text(document, old_text, new_text):
    for paragraph in document.paragraphs:
        # print("0paragraph.text"+paragraph.text)
        paragraph.text = paragraph.text.replace(old_text, str(new_text))
        # print("1paragraph.text"+paragraph.text)

# 更新模板内容
# 示例：根据数据中的特定列更新 Word 中的占位符
print("data"+data.columns)
i = 0
for row in data.iterrows():
    document = Document(template_file)
    i=i+1
    for col in data.columns:
        print("col",col)
        name1 = row[1][col]
        print("name1",name1)
        replace_text(document,col,name1)

    # name = "afei"
    # 生成新的 Word 文件
    new_file = "new_word_file"+str(i)+".docx"
    document.save(new_file)




# 完成！
print("新的 Word 文件已生成：", new_file)